﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzukiMap.Layer;
using AzukiMap.Renderer;

namespace AzukiMapUI
{
	public partial class LayerExpressionSelect : UserControl
	{
        const string DEFAULT_EXPRESSION_STR = " ";
        IVectorLayer _layer;

        public IVectorLayer VectorLayer
        {
            set {
                _layer = value;
                AddItemsByVectorLayer(value);
                SetVectorType(value);
            }
        }

		public LayerExpressionSelect()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        void AddItemsByVectorLayer(IVectorLayer layer)
        {
            LbExpressionSelect.Items.Clear();

            if (layer.DefaultStyle == null)
            {
                return;
            }
            // Add a default style item.
            TextBlock tbkDefaultExpression = new TextBlock();
            tbkDefaultExpression.Text = "Default";
            tbkDefaultExpression.Resources.Add("VectorStyle", layer.DefaultStyle);
            LbExpressionSelect.Items.Add(tbkDefaultExpression);

            // Add layer theme style items.
            foreach (VectorLayerTheme theme in layer.LayerThemes)
            {
                TextBox tbExpression = new TextBox();
                tbExpression.Text = theme.Expression;
                tbExpression.Resources.Add("Theme", theme);
                tbExpression.Resources.Add("VectorStyle", theme.Style);
                LbExpressionSelect.Items.Add(tbExpression);
            }
        }

        void AddNewItem()
        {
            TextBox tbExpression = new TextBox();
            tbExpression.Text = DEFAULT_EXPRESSION_STR;
            VectorStyle style = new VectorStyle();
            VectorLayerTheme theme = new VectorLayerTheme(DEFAULT_EXPRESSION_STR, style);
            _layer.LayerThemes.Add(theme);

            tbExpression.Resources.Add("Theme", theme);
            tbExpression.Resources.Add("VectorStyle", theme.Style);
            tbExpression.LostFocus += new RoutedEventHandler(tbExpression_LostFocus);
            LbExpressionSelect.Items.Add(tbExpression);
        }

        void tbExpression_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tbExpression = (TextBox)sender;
            string inputStr = tbExpression.Text;
            if (inputStr == DEFAULT_EXPRESSION_STR || inputStr == "")
            {
                return;
            }
            VectorLayerTheme theme = (VectorLayerTheme)tbExpression.Resources["Theme"];
            theme.Expression = inputStr;
        }

        void SetVectorType(IVectorLayer layer)
        {
            StyleSelectPart.VectorType = layer.VectorType;
        }
        
        private void IconAddExpression_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AddNewItem();
        }

        private void LbExpressionSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FrameworkElement selectedExpression = (FrameworkElement)((ListBox)sender).SelectedItem;
            //VectorLayerTheme theme = (VectorLayerTheme)selectedExpression.Resources["Theme"];
            // IVectorStyle selectedStyle = theme.Style;
            IVectorStyle selectedStyle = (IVectorStyle)selectedExpression.Resources["VectorStyle"];
            StyleSelectPart.Style = selectedStyle;
            StyleSelectPart.Visibility = Visibility.Visible;
        }
	}
}